package ws.db.sql;


/**
 * SelectSQL class provide the base temple select SQL.
 * Not provide select SQL which has no columns-name.
 * Such as this "select * from table" SQL is not provide.
 * */
public class SelectSQL extends SQL {

	protected String[] columns;
	
	
	public SelectSQL(String m_table,String[] m_columns){
		super(m_table);
		columns = m_columns;
	}
	
	
	public SelectSQL(String m_table, String m_where,
							String[] m_columns){
		this(m_table,m_columns);
		where = m_where;
	}
	
	public SelectSQL(String m_table,String m_where,String m_order,
								String[] m_columns){
		this(m_table,m_where,m_columns);
		order = m_order;
	}
	
	/**
	 * Select SQL needn`t use the values so the second param is String array.
	 * @param:
	 * 		sql - 
	 * */
	protected void constructColumns(StringBuffer sql,String[] columns){
		
		if(columns==null){
			sql.append(" * ");
		}
		for(int i=0; i<columns.length; i++)
		{
			sql.append(columns[i]);
			if(i!=columns.length-1)
			{
				sql.append(",");
				
			}
		}
		
	}
	/**
	 * Provide select SQL such as "select column1,column2 from table where ... order...".
	 * @param:
	 * 		void;
	 * @return:
	 * 		String - the select SQL.
	 * 
	 * */
	@Override
	public String getSQL() {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer();
		sql.append("select ");
		constructColumns(sql,columns);
		sql.append(" from ");
		sql.append(table);
		dealwithWhereOrder(sql,where,order);
		sql.append(";");
		return sql.toString();
		
	}
	
	
}
